*****************************************************
*****************************************************
* Analysis of transaction vs. actual prices
*****************************************************
*****************************************************

use "${FINALDATA}/immo_transaction", clear

if ($LOGTIME == 1) log using "${LOG}/codebook_immo_transaction.log", replace
	codebook, compact
log close


** recode so that monat captures month of transaction
sort matchid transaction
bysort matchid (transaction): replace monat = monat[_n+1] if _n == 1

global ifnotlastmonth `"if monat != monthly("2015 10", "YM")"' 


*****************************************************
* Figure 6 (Online Appendix B): Transaction and Asking Prices Over Time
*****************************************************
order obid matchid transaction kaufpreis
sort matchid

gen lkaufpreis = log(kaufpreis)

tab transaction monat

table transaction, c(mean kaufpreis sem kaufpreis)
table monat transaction, c(mean lkaufpreis)

local savename xsection_transaction_offer_price
local xlin2 = monthly("2014 05","YM")-0.5
preserve
	collapse (mean) lkaufpreis (sem) lkaufpreis_sem = kaufpreis, by(transaction monat)
	twoway (line lkaufpreis monat $ifnotlastmonth & transaction == 1, xline(`xlin2')) ///
			(line lkaufpreis monat $ifnotlastmonth & transaction == 0, lpattern(longdash)) ///
			, xtitle("") ytitle("Average price, in logs") ///
			tlabel(2013m5(6)2015m5, format(%tmMonYY)) legend(rows(1) label(1 "Transaction prices")  label(2 "Asking prices") /* label(4 "Private sellers") */ /* label(5 "Housing industry") */) ///
			xmtick(##6) ///
			graphregion(color(white)) ///
			saving("$GRAPHS/`savename'", replace)
	graph export "$GRAPHS/`savename'.pdf", replace
restore






cap log close
if ($LOGTIME == 1) log using "${LOG}/${DATE}_analyses_transaction_offerprice.log", replace


* number rof matched observations (not in last month)
distinct obid $ifnotlastmonth & transaction == 0 

*****************************************************
** Table 8 (Online Appendix B): Estimation Results on the Difference between logged Transaction and logged Asking Prices
*****************************************************
cap drop lkaufpreis_diff
sort matchid notransaction
bysort matchid (notransaction): gen lkaufpreis_diff = lkaufpreis - lkaufpreis[_n+1]   if _n==1	

reg lkaufpreis_diff i.postmai $ifnotlastmonth , vce(robust)
est sto interact

* only around cutoff (652 is May 2014)
reg lkaufpreis_diff i.postmai if inrange(monat,`=652-3',`=651+3'), vce(cluster matchid)
est sto interact_m3

*reg lkaufpreis_diff i.postmai if inrange(monat,651,652), vce(cluster matchid)
reg lkaufpreis_diff i.postmai if inrange(monat,`=652-6',`=651+6'), vce(cluster matchid)
est sto interact_m6

*reg lkaufpreis_diff i.postmai if inrange(monat,651,652), vce(cluster matchid)
reg lkaufpreis_diff i.postmai if inrange(monat,`=652-12',`=651+12'), vce(cluster matchid)
est sto interact_m12


*** tables for paper
estout interact interact_m3 interact_m6 interact_m12, ///
	cells(b(fmt(3) star) se(fmt(3)) /*ci(fmt(3))*/   ) ///
	stats(N N_clust, fmt(%9.0fc %9.0fc)) ///
	varwidth(12) starlevels(* 0.05 ** 0.01) ///
	drop( ) style(tex) nobase
	
log close
